package com.onesports.intelligent.k12.polarlight.controller.organization.report;

import cn.gjing.excel.executor.ExcelFactory;
import com.onesports.framework.kit.common.valid.Json;
import com.onesports.intelligent.k12.polarlight.common.utils.ResultUtil;
import com.onesports.intelligent.k12.polarlight.constant.ApiPathConstants;
import com.onesports.intelligent.k12.polarlight.domain.dto.report.FinanceQueryDTO;
import com.onesports.intelligent.k12.polarlight.domain.vo.market.TotalDataVO;
import com.onesports.intelligent.k12.polarlight.domain.vo.report.FinanceVO;
import com.onesports.intelligent.k12.polarlight.security.SecurityUtils;
import com.onesports.intelligent.k12.polarlight.service.order.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Api(tags = "机构端-财务管理-财务统计")
@RestController
@RequiredArgsConstructor
@RequestMapping(ApiPathConstants.ORGANIZATION_FINANCE)
public class FinanceController {

    private final OrderService orderService;

    @ApiOperation("查询")
    @GetMapping("/page")
    public ResponseEntity<TotalDataVO<FinanceVO>> page(@Json FinanceQueryDTO dto){
        return ResultUtil.success(orderService.selectFinancePage(dto, SecurityUtils.getOrganizationId()));
    }

    @ApiOperation("导出")
    @GetMapping("/export")
    public void export(HttpServletResponse response, FinanceQueryDTO dto) throws UnsupportedEncodingException {
        List<FinanceVO> data = orderService.financeExport(dto,SecurityUtils.getOrganizationId());
        ExcelFactory.createWriter(FinanceVO.class, response)
                .write(data).flush();
    }
}
